from marshmallow import fields

from extensions import marshmallow as ma


class CreateUserParam(ma.Schema):
    screen_name = fields.Str()
    head_image = fields.Str()
    phone = fields.Str()
    email = fields.Str()


class CreateAuthParam(ma.Schema):
    login_name = fields.Str(required=True)
    password = fields.Str(required=True)
    user_info = fields.Nested(CreateUserParam)
    channel = fields.Str()
    # x = fields.Str(required=True)


class ParamError(ma.Schema):
    message = fields.Str()
    extra_info = fields.Dict()
    code = fields.Str()

    def __init__(self, code='909090', message='Param Error', extra={}):
        ma.Schema.__init__(self, many=False)

        self.message = message
        self.code = code
        self.extra_info = extra

    @classmethod
    def jsonify_with(cls, code=None, message=None, extra={}, *args, **kwargs):
        error = ParamError(code, message, extra)
        return error.jsonify(*args, **kwargs)

    def jsonify(self, *args, **kwargs):
        return super().jsonify({
            'message': self.message,
            'code': self.code,
            'extra_info': self.extra_info
        }, *args, **kwargs) ,400


create_auth_param = CreateAuthParam()

login_name_is_exist = ParamError(
    code='100000',
    message='登录名已经存在',
    extra={
        'login_name': 'login_name_exist'
    }
)
